package com.download.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.download.web.entity.SysNavBar;
import com.download.web.entity.SysSponsor;
import com.download.web.entity.SysUser;
import com.download.web.entity.model.ArticleListModel;
import com.download.web.entity.model.CommentModel;
import com.download.web.entity.model.Page;
import com.download.web.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 前端首页
 * @author: cmy
 * @create: 2019-02-22 14:10
 **/
@Controller
public class WebIndexController {
    @Autowired
    private ISysNavBarService navBarService;
    @Autowired
    private IBusArticleService articleService;

    @Autowired
    private ISponsorService sponsorService;
    @Autowired
    private IBusArticleCommentService articleCommentService;
    @Autowired
    private IBusUserHistoryService userHistoryService;
    @Autowired
    private ISysUserService userService;

    @RequestMapping("/")
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("index");
        //导航栏
        List<Map<String,Object>> navList=new ArrayList<>();
        List<SysNavBar> navBarList=navBarService.list(new QueryWrapper<SysNavBar>().eq("parent_id",0));
        navBarList.forEach(n->{
            List<SysNavBar> childNavBarList=navBarService.list(new QueryWrapper<SysNavBar>().eq("parent_id",n.getId()));
            Map<String,Object> map=new HashMap<>();
            map.put("nav",n);
            map.put("childNav",childNavBarList);
            navList.add(map);
        });
//        mv.addObject("navBarList", navList);
        request.getSession().setAttribute("navBarList", navList);
        //文章列表
        Page<ArticleListModel> articles=articleService.articlePage(0,30,0);
        mv.addObject("articles", articles);
        //轮播图文章列表
        Page<ArticleListModel> ratopleArts=articleService.articlePage(0,10,1);
        mv.addObject("ratopleArts", ratopleArts);
        //简单文章(序列号)列表
        Page<ArticleListModel> simpleArts=articleService.articlePageByCategory(0,24,2);
        mv.addObject("simpleArts", simpleArts);
        return mv;
    }

    @RequestMapping("/aboutus")
    public String aboutus() {
        return "web/aboutus";
    }
    @RequestMapping("/copyright")
    public String copyright() {
        return "web/copyright";
    }

    @RequestMapping("/sponsor")
    public ModelAndView sponsor(){
        ModelAndView mv = new ModelAndView("web/xpay");
        SysSponsor sponsor=sponsorService.getById(1);
        mv.addObject("article",sponsor);
        Page<CommentModel> commentModelPage=articleCommentService.commentListPage(sponsor.getId(),0,5,2);
        mv.addObject("comments",commentModelPage);
        return mv;
    }

    @RequestMapping("/mine")
    public ModelAndView mine(@RequestParam(value = "pageIndex",defaultValue = "0") Integer pageIndex,
                             @RequestParam(value = "pageSize",defaultValue = "30") Integer pageSize,
                             Integer userId){
        ModelAndView mv=new ModelAndView("web/mine");
        mv.addObject("articles",userHistoryService.articleHistoryList(pageIndex,pageSize,userId));
        mv.addObject("user",userService.getById(userId));
        return mv;
    }

    @RequestMapping("/vip")
    public String vip(HttpServletRequest request,HttpServletResponse response) throws IOException {
        if (!isLogin( request, response)){
            return "redirect:/download/sys-user/toLogin";
        }
        return "web/vip";
    }


    @RequestMapping("/installationWrong")
    public String installationWrong(){
        return "web/installation-wrong";
    }

    @RequestMapping("/resetPwd")
    public String resetPwd(HttpServletRequest request,HttpServletResponse response) throws IOException {
        if (!isLogin( request, response)){
            return "redirect:/download/sys-user/toLogin";
        }
        return "web/reset-pwd";
    }
    @RequestMapping("/findPwd")
    public String findPwd() throws IOException {
        return "web/find-pwd";
    }



    public boolean isLogin(HttpServletRequest request,HttpServletResponse response) throws IOException {
        SysUser user=(SysUser)request.getSession().getAttribute("user");
        if (user==null){ return false; }
        return true;
    }

    @RequestMapping("/tosi")
    public String paylist(){
        return "web/404";
    }

    @RequestMapping("/server")
    @ResponseBody
    public String server(HttpServletRequest request){
        return request.getRealPath("/");
    }
}
